package com.dunqi.gpm.shunqing.develop.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.TextView;

import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.base.ComObserver;
import com.dunqi.gpm.shunqing.common.base.Data;
import com.dunqi.gpm.shunqing.common.util.WebUtil;
import com.dunqi.gpm.shunqing.develop.bean.ProjectNews;
import com.dunqi.gpm.shunqing.home.activity.InstructionsActivity;
import com.dunqi.gpm.shunqing.home.bean.ProjectWorkTrend;
import com.dunqi.gpm.shunqing.home.constance.Constance;
import com.dunqi.gpm.shunqing.home.widget.NormalView;
import com.dunqi.gpm.shunqing.my.util.UserUtil;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

public class WorkDevelopDetailActivity extends BaseAct {

    private static final String DATA = "data";
    @BindView(R.id.send_company_view)
    NormalView sendCompanyView;
    @BindView(R.id.send_time_view)
    NormalView sendTimeView;
    @BindView(R.id.title_view)
    NormalView titleView;
    @BindView(R.id.web_view)
    WebView web_view;
    @BindView(R.id.leave_message_tv)
    TextView leaveMessageTv;
    private ProjectNews projectNews;

    ProjectWorkTrend item;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        if (projectNews.getObjectId() == null) {
            return;
        }
        ApiFactory.getInstance()
                .getProjectWorkTrendInfo(projectNews.getObjectId())
                .compose(RxSchedulers.<Data<ProjectWorkTrend>>io_main())
                .subscribe(new ComObserver<ProjectWorkTrend>() {
                    @Override
                    public void onSuccess(ProjectWorkTrend trend) {
                        item = trend;
                        sendCompanyView.setText(trend.getIssueOrgName());
                    }

                    @Override
                    public void onFinal() {

                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposables(d);
                    }
                });

    }

    @Override
    protected void initUiAndListener() {

        projectNews = (ProjectNews) getIntent().getSerializableExtra(DATA);

        sendTimeView.setText(projectNews.getCreateTime());
        titleView.setText(projectNews.getTitle());
        String content = projectNews.getDetail();
        String baseUrl = UserUtil.getBaseUrl();
        String url = baseUrl + content;
        WebUtil.webSetting(web_view, this, false);
        web_view.loadUrl(url);


        int levelType = UserUtil.getLevelType();
        if (UserUtil.userIsLeader()) {
            leaveMessageTv.setText("批示");
        } else {
            leaveMessageTv.setText("留言");
        }

    }


    public static void start(Context context, ProjectNews projectNews) {
        Intent intent = new Intent(context, WorkDevelopDetailActivity.class);
        intent.putExtra(DATA, projectNews);
        context.startActivity(intent);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_work_develop_detail;
    }


    @OnClick(R.id.leave_message_tv)
    public void onViewClicked() {
        if (item == null) {
            return;
        }

        int msgType;
        if (UserUtil.userFromRespOrg() || UserUtil.userFromNdrcOrg()) {
            msgType = 2;
        } else {
            msgType = 1;
        }

        InstructionsActivity.start(this, item.getProWorkTrendId(), Constance.INSTRUCTION_TYPE_PROJECT_WORK_TREND, msgType, null);

    }
}
